package com.fh.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fh.model.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.servlet.View;

/**
 * 返回数据
 * @author 
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	public R() {
		put("code", 200);
	}
	
	public static R error() {
		return error(500, "未知异常，请联系管理员");
	}
	
	public static R error(String msg) {
		return error(500, msg);
	}
	
	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}
	
	public static R error(int code, String msg,String detail) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		r.put("detail", detail);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}
	
	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}
	
	public static R ok() {
		return new R();
	}

	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}
	
	
	//分页
	public static void setPageHelper(PageData pageData){
		String showCount =  pageData.getString("page.showCount");
		String currentPage =  pageData.getString("page.currentPage");
		PageHelper.startPage(currentPage == null ? 1 : Integer.valueOf(currentPage),
				showCount == null ? 10 : Integer.valueOf(showCount));
		
	}
	
	public static void setPage(Page page,List list){
		PageInfo pageInfo= new PageInfo(list);
		page.setShowCount(pageInfo.getPageSize());
		page.setTotalPage(pageInfo.getPages());
		page.setTotalResult(new Long(pageInfo.getTotal()).intValue());
		page.setCurrentPage(pageInfo.getPageNum());
	}
	
	public static void setJqGridPageHelper(PageData pageData){
		PageHelper.startPage(Integer.valueOf(pageData.getString("currentPage")),
				Integer.valueOf(pageData.getString("showCount")));
		
	}
	public static Map<String ,Object> setJqGridPage(List list){
		PageInfo pageInfo= new PageInfo(list);
		Map<String ,Object> result = new HashMap<>();
		//总记录数
		result.put("records",pageInfo.getTotal());
		//当前页
		result.put("page",pageInfo.getPageNum());
		//共多少页
		result.put("total",pageInfo.getPages());
		result.put("rows",pageInfo.getList());
		return  result;
	}
	
}
